<?php

namespace app\system\controller;

use app\admin\controller\Common;
use app\common\model\Files;

class Component extends Common
{
    /**
     *上传文件
     */
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
//        halt();
        $info = $file->move('../public/uploads');
        if ($info) {
            $old_info = $info->getinfo();
            $data = [
                'name' => $old_info['name'],
                'filename' => $info->getFilename(),
                'path' => 'uploads/' . $info->getSaveName(),
                'extension' => $info->getExtension(),
                'size' => $info->getSize(),
                'author' => session('admin.id'),
            ];
            $res = Files::create($data);
            if ($res){
                return ['code' => 1, 'message' => '上传成功','data' => $res];
            }else{
                return ['code' => 0, 'message' => '上传失败'];
            }
        } else {
            // 上传失败获取错误信息
//            echo $file->getError();
            return ['code' => 0, 'message' => $file->getError()];
        }
    }
}
